<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    //1.数组中存放的是原始类型
    var names = ["abc","cba","nba"]

    // 1.1 indexOf方法
    names.indexOf("abc")


    var students = [
      {id:101,name:"zhangsan"},
      {id:102,name:"lisi"},
      {id:103,name:"wangwu"},
      {id:104,name:"zhaoliu"},
    ]

    // 2 如果存放的是对象类型,可以使用find方法
    var stu = students.find(function(item){
      return item.id === 105
    })
    console.log(stu);
    


    // 自己实现find方法
    // function myFind(lst,fn){
    //   for (const item of lst) {
    //     const result = fn(item)
    //     if (result) {
    //       return item
    //     }
    //   }
    // }

    // const result = myFind(students,function(item){
    //   return item.id === 101
    // })

    // console.log(result)

    Array.prototype.myFind = function myFind(fn){
      for (const item of this) {
        const result = fn(item)
        if (result) {
          return item
        }
      }
    }
    var stu = students.myFind(function(item){
      return item.id === 101
    })
    console.log(stu);
  </script>
</body>
</html>